#include "TestScreen.h"

#include "GameDefine.h"
using namespace cocos2d;


void CTestScreen::onEnter()
{
	this->setIsTouchEnabled(true);
	this->setIsAccelerometerEnabled(true);

#ifdef TARGET_IPHONE4
	setAnchorPoint(ccp(0, 0));
	setScaleX(GAME_SCALEX);
	setScaleY(GAME_SCALEY);
	setPosition(ccp(240 - GAME_WIDTH*GAME_SCALEX*0.5f, 0));
#endif

	CCSprite *pSprBack = CCSprite::spriteWithFile("scence/back.jpg");
	pSprBack->setPosition(ccp(GAME_WIDTH * 0.5f, GAME_HEIGHT*0.5f));
	addChild(pSprBack);

	//m_pSprBow = CSpriteBow::spriteWithFile("bow.png");
	//m_pSprBow->SetLinePara(ccp(51, 14), ccp(51, 104), ccp(51, 59));
	//m_pSprBow->setPosition(ccp(500, 400));
	//addChild(m_pSprBow);
	InitUI();
	//m_player.InitState(this);

	m_lancer.InitState(this);

	schedule(schedule_selector(CTestScreen::OnUpdate));

	CCLayer::onEnter();
}

//---------------------------------------------------------------------------------------------------/
void CTestScreen::onExit()
{
	CCLayer::onExit();
}

//---------------------------------------------------------------------------------------------------/
void CTestScreen::didAccelerate(CCAcceleration* pAccelerationValue)
{
	m_ptCurAcc.x = (float)pAccelerationValue->x;
	m_ptCurAcc.y = (float)pAccelerationValue->y;
}

//---------------------------------------------------------------------------------------------------/
void CTestScreen::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	if(pTouches->count() < 1)
	{
		return;
	}

	int count = pTouches->count();

	CCTouch *pTouch = (CCTouch*)pTouches->anyObject();

	//m_pSprBow->SetLinePara(ccp(51, 14), ccp(51, 104), m_pSprBow->convertTouchToNodeSpace(pTouch));
	//m_pSprBow->Touched(CCPointZero);

}

void CTestScreen::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
	if(pTouches->count() < 1)
	{
		return;
	}

	int count = pTouches->count();

	CCTouch *pTouch = (CCTouch*)pTouches->anyObject();

	//m_pSprBow->SetLinePara(ccp(51, 14), ccp(51, 104), m_pSprBow->convertTouchToNodeSpace(pTouch));

}

//---------------------------------------------------------------------------------------------------/
void CTestScreen::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
	//m_pSprBow->TouchedCancle();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CTestScreen::onButtonClick(int aID)
{
	switch(aID)
	{
	case ID_BTN_ATTACK:
		{
			int a = rand() % 2;

			if(0 == a)
			{	
				m_lancer.Split();
			}
			else if(1 == a)
			{
				m_lancer.Prick();
			}
			else if(2 == a)
			{
				//m_lancer.Attacked();
			}
			
			//m_lancer.Prick();
			//m_lancer.DeadFly();
			//m_lancer.DeadOrigin();
			//m_lancer.Attacked();
		}
		break;
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CTestScreen::InitUI()
{
	m_pBtnAttack = CSpriteButton::spriteWithFile("ui/joy.png");
	m_pBtnAttack->SetData(ID_BTN_ATTACK, this);
	m_pBtnAttack->SetIsEnable(true);
	m_pBtnAttack->setPosition(ccp(825, 120));
	addChild(m_pBtnAttack, 10);


	m_pJoy = CCocosJoystick::CreateJoystick(this, 20, "ui/joy.png", "ui/joybk.png", true, CCRect(0, 0, GAME_WIDTH*0.5f, GAME_HEIGHT*0.5f));
	m_pJoy->setPosition(ccp(100, 100));

}

void CTestScreen::OnUpdate(cocos2d::ccTime dt)
{
	//m_player.Update(dt);

	m_lancer.Move(ccp(m_pJoy->GetCurPowerX(), m_pJoy->GetCurPowerY()));
}